﻿using System;
using System.Diagnostics;
using System.Net;
using System.Net.Sockets;
using System.Runtime.CompilerServices;
using System.Threading;
using HslCommunication.Core.Net;
using HslCommunication.LogNet;

namespace HslCommunication.Enthernet
{
	/// <summary>
	/// 消息处理服务器，主要用来实现接收客户端信息并进行消息反馈的操作，适用于客户端进行远程的调用，要求服务器反馈数据。<br />
	/// The message processing server is mainly used to implement the operation of receiving client information and performing message feedback. It is applicable to remote calls made by clients and requires the server to feedback data.
	/// </summary>
	/// <remarks>
	/// 详细的使用说明，请参照博客<a href="http://www.cnblogs.com/dathlin/p/7697782.html">http://www.cnblogs.com/dathlin/p/7697782.html</a>
	/// </remarks>
	/// <example>
	/// 此处贴上了Demo项目的服务器配置的示例代码
	/// <code lang="cs" source="TestProject\SimplifyNetTest\FormServer.cs" region="Simplify Net" title="NetSimplifyServer示例" />
	/// </example>
	// Token: 0x020000FF RID: 255
	public class NetSimplifyServer : NetworkAuthenticationServerBase
	{
		/// <summary>
		/// 接收字符串信息的事件
		/// </summary>
		// Token: 0x14000030 RID: 48
		// (add) Token: 0x06001552 RID: 5458 RVA: 0x0006EBD0 File Offset: 0x0006CDD0
		// (remove) Token: 0x06001553 RID: 5459 RVA: 0x0006EC08 File Offset: 0x0006CE08
		[DebuggerBrowsable(DebuggerBrowsableState.Never)]
		public event Action<AppSession, NetHandle, string> ReceiveStringEvent;

		/// <summary>
		/// 接收字符串数组信息的事件
		/// </summary>
		// Token: 0x14000031 RID: 49
		// (add) Token: 0x06001554 RID: 5460 RVA: 0x0006EC40 File Offset: 0x0006CE40
		// (remove) Token: 0x06001555 RID: 5461 RVA: 0x0006EC78 File Offset: 0x0006CE78
		[DebuggerBrowsable(DebuggerBrowsableState.Never)]
		public event Action<AppSession, NetHandle, string[]> ReceiveStringArrayEvent;

		/// <summary>
		/// 接收字节信息的事件
		/// </summary>
		// Token: 0x14000032 RID: 50
		// (add) Token: 0x06001556 RID: 5462 RVA: 0x0006ECB0 File Offset: 0x0006CEB0
		// (remove) Token: 0x06001557 RID: 5463 RVA: 0x0006ECE8 File Offset: 0x0006CEE8
		[DebuggerBrowsable(DebuggerBrowsableState.Never)]
		public event Action<AppSession, NetHandle, byte[]> ReceivedBytesEvent;

		/// <summary>
		/// 向指定的通信对象发送字符串数据
		/// </summary>
		/// <param name="session">通信对象</param>
		/// <param name="customer">用户的指令头</param>
		/// <param name="str">实际发送的字符串数据</param>
		// Token: 0x06001558 RID: 5464 RVA: 0x0006ED1D File Offset: 0x0006CF1D
		public void SendMessage(AppSession session, int customer, string str)
		{
			base.Send(session.WorkSocket, HslProtocol.CommandBytes(customer, base.Token, str));
		}

		/// <summary>
		/// 向指定的通信对象发送字符串数组
		/// </summary>
		/// <param name="session">通信对象</param>
		/// <param name="customer">用户的指令头</param>
		/// <param name="str">实际发送的字符串数组</param>
		// Token: 0x06001559 RID: 5465 RVA: 0x0006ED39 File Offset: 0x0006CF39
		public void SendMessage(AppSession session, int customer, string[] str)
		{
			base.Send(session.WorkSocket, HslProtocol.CommandBytes(customer, base.Token, str));
		}

		/// <summary>
		/// 向指定的通信对象发送字节数据
		/// </summary>
		/// <param name="session">连接对象</param>
		/// <param name="customer">用户的指令头</param>
		/// <param name="bytes">实际的数据</param>
		// Token: 0x0600155A RID: 5466 RVA: 0x0006ED55 File Offset: 0x0006CF55
		public void SendMessage(AppSession session, int customer, byte[] bytes)
		{
			base.Send(session.WorkSocket, HslProtocol.CommandBytes(customer, base.Token, bytes));
		}

		/// <summary>
		/// 关闭网络的操作
		/// </summary>
		// Token: 0x0600155B RID: 5467 RVA: 0x0006ED71 File Offset: 0x0006CF71
		protected override void CloseAction()
		{
			this.ReceivedBytesEvent = null;
			this.ReceiveStringEvent = null;
			base.CloseAction();
		}

		/// <summary>
		/// 当接收到了新的请求的时候执行的操作
		/// </summary>
		/// <param name="socket">异步对象</param>
		/// <param name="endPoint">终结点</param>
		// Token: 0x0600155C RID: 5468 RVA: 0x0006ED8C File Offset: 0x0006CF8C
		protected override void ThreadPoolLogin(Socket socket, IPEndPoint endPoint)
		{
			AppSession appSession = new AppSession();
			appSession.WorkSocket = socket;
			try
			{
				appSession.IpEndPoint = endPoint;
				appSession.IpAddress = appSession.IpEndPoint.Address.ToString();
			}
			catch (Exception ex)
			{
				ILogNet logNet = base.LogNet;
				if (logNet != null)
				{
					logNet.WriteException(this.ToString(), StringResources.Language.GetClientIpAddressFailed, ex);
				}
			}
			ILogNet logNet2 = base.LogNet;
			if (logNet2 != null)
			{
				logNet2.WriteDebug(this.ToString(), string.Format(StringResources.Language.ClientOnlineInfo, appSession.IpEndPoint));
			}
			try
			{
				appSession.WorkSocket.BeginReceive(new byte[0], 0, 0, SocketFlags.None, new AsyncCallback(this.ReceiveCallback), appSession);
				Interlocked.Increment(ref this.clientCount);
			}
			catch (Exception ex2)
			{
				Socket workSocket = appSession.WorkSocket;
				if (workSocket != null)
				{
					workSocket.Close();
				}
				ILogNet logNet3 = base.LogNet;
				if (logNet3 != null)
				{
					logNet3.WriteException(this.ToString(), StringResources.Language.NetClientLoginFailed, ex2);
				}
			}
		}

		// Token: 0x0600155D RID: 5469 RVA: 0x0006EEAC File Offset: 0x0006D0AC
		[DebuggerStepThrough]
		private void ReceiveCallback(IAsyncResult ar)
		{
			NetSimplifyServer.<ReceiveCallback>d__15 <ReceiveCallback>d__ = new NetSimplifyServer.<ReceiveCallback>d__15();
			<ReceiveCallback>d__.<>t__builder = AsyncVoidMethodBuilder.Create();
			<ReceiveCallback>d__.<>4__this = this;
			<ReceiveCallback>d__.ar = ar;
			<ReceiveCallback>d__.<>1__state = -1;
			<ReceiveCallback>d__.<>t__builder.Start<NetSimplifyServer.<ReceiveCallback>d__15>(ref <ReceiveCallback>d__);
		}

		/// <summary>
		/// 让客户端正常下线，调用本方法即可自由控制会话客户端强制下线操作。
		/// </summary>
		/// <param name="session">会话对象</param>
		// Token: 0x0600155E RID: 5470 RVA: 0x0006EEEC File Offset: 0x0006D0EC
		public void AppSessionRemoteClose(AppSession session)
		{
			Socket workSocket = session.WorkSocket;
			if (workSocket != null)
			{
				workSocket.Close();
			}
			Interlocked.Decrement(ref this.clientCount);
			ILogNet logNet = base.LogNet;
			if (logNet != null)
			{
				logNet.WriteDebug(this.ToString(), string.Format(StringResources.Language.ClientOfflineInfo, session.IpEndPoint));
			}
		}

		/// <summary>
		/// 当前在线的客户端数量
		/// </summary>
		// Token: 0x170004FE RID: 1278
		// (get) Token: 0x0600155F RID: 5471 RVA: 0x0006EF45 File Offset: 0x0006D145
		public int ClientCount
		{
			get
			{
				return this.clientCount;
			}
		}

		/// <inheritdoc />
		// Token: 0x06001560 RID: 5472 RVA: 0x0006EF4D File Offset: 0x0006D14D
		public override string ToString()
		{
			return string.Format("NetSimplifyServer[{0}]", base.Port);
		}

		// Token: 0x0400051B RID: 1307
		private int clientCount = 0;
	}
}
